#include "display_setting_menu.h"
#include "direction_menu.h"
//#include "brightness_menu.h"
#include "language_menu.h"
#include "menu_config.h"

/* 添加全屏刷新标志位 */
static bool needFullRefresh = true;
/* 显示刷新标志，决定是否需要重绘菜单界面 */
static bool sg_needRefresh = true;
/* 记录上次选中的菜单项索引，用于高亮和刷新 */
static uint8_t sg_lastSelectItem = 0;

/* 返回按钮回调函数 */
static void OnBackEnter(const cotMenuItemInfo_t *ptItemShowInfo)
{
    /* 返回上级菜单 */
    cotMenu_Exit(false);
    cotMenu_Exit(false);
}

/* DisplaySetting菜单项定义 */
cotMenuList_t sg_DisplaySettingMenuTable[] =
{
    COT_MENU_ITEM_BIND(TEXT_DIRECTION, OnDirectionMenuEnter, NULL, LoadDirectionMenu, NULL, "Direction"),
//    COT_MENU_ITEM_BIND(TEXT_BRIGHTNESS, OnBrightnessMenuEnter, NULL, LoadBrightnessMenu, NULL, "Brightness"),
    COT_MENU_ITEM_BIND(TEXT_LANGUAGE, OnLanguageMenuEnter, NULL, LoadLanguageMenu, NULL, "Language"),
    COT_MENU_ITEM_BIND(TEXT_BACK, OnBackEnter, NULL, NULL, NULL, "Back"),
};

/* 显示函数 */
static void ShowDisplaySettingMenu(cotMenuShow_t *ptShowInfo)
{
    /* 如果不需要刷新显示，直接返回 */
    if (!sg_needRefresh)
    {
        return;
    }
    
    uint8_t showNum = MENU_MAX_SHOW_NUM; /* 每页最多显示的菜单项数（自动根据屏幕和字体大小计算） */
    static int lastStartIndex = -1;

    /* 限制显示的菜单项数量，防止越界 */
    cotMenu_LimitShowListNum(ptShowInfo, &showNum);

    /* 计算当前页起始菜单项索引 */
    uint8_t startIndex = (ptShowInfo->selectItem / showNum) * showNum;

    if (startIndex != lastStartIndex)
    {
        needFullRefresh = true;
        lastStartIndex = startIndex;
    }

    if (needFullRefresh)
    {
        /* 清屏 */
        lcd_clear(g_back_color);

        /* 显示菜单标题，居中显示 */
        const char *pszSelectDesc = get_text((TextId_e)ptShowInfo->uMenuDesc.textId);
        text_show_string_middle(0, 0, (char *)pszSelectDesc, MENU_FONT_SIZE, lcddev.width, g_point_color);

        /* 在标题下方画一条横线 */
        lcd_draw_line(0, MENU_FONT_SIZE, lcddev.width - 1, MENU_FONT_SIZE, g_point_color);

        /* 首次显示时，刷新所有菜单项 */
        for (uint16_t i = 0; i < showNum; i++)
        {
            uint8_t currentIndex = startIndex + i;
            if (currentIndex < COT_GET_MENU_NUM(sg_DisplaySettingMenuTable))
            {
                const char *pItemName = get_text((TextId_e)ptShowInfo->uItemsListDesc[currentIndex].textId);
                uint16_t y = CALC_MENU_ITEM_Y(i); /* 计算每项的y坐标 */
                if (currentIndex == ptShowInfo->selectItem)
                {
                    /* 选中项反色显示 */
                    text_show_menu_item(0, y, (char *)pItemName, MENU_FONT_SIZE, g_back_color, g_point_color);
                }
                else
                {
                    /* 普通项正常显示 */
                    text_show_menu_item(0, y, (char *)pItemName, MENU_FONT_SIZE, g_point_color, g_back_color);
                }
            }
        }
        needFullRefresh = false;
    }

    /* 只更新发生变化的菜单项 */
    if (sg_lastSelectItem != ptShowInfo->selectItem)
    {
        /* 计算上次选中项在当前页的行号 */
        uint8_t lastRow = sg_lastSelectItem - startIndex;
        if (lastRow < showNum && sg_lastSelectItem < COT_GET_MENU_NUM(sg_DisplaySettingMenuTable))
        {
            /* 取消旧高亮，恢复为普通显示 */
            const char *pItemName = get_text((TextId_e)ptShowInfo->uItemsListDesc[sg_lastSelectItem].textId);
            uint16_t y = CALC_MENU_ITEM_Y(lastRow);
            text_show_menu_item(0, y, (char *)pItemName, MENU_FONT_SIZE, g_point_color, g_back_color);
        }
        
        /* 计算当前选中项在当前页的行号 */
        uint8_t currRow = ptShowInfo->selectItem - startIndex;
        if (currRow < showNum && ptShowInfo->selectItem < COT_GET_MENU_NUM(sg_DisplaySettingMenuTable))
        {
            /* 高亮当前选中项 */
            const char *pItemName = get_text((TextId_e)ptShowInfo->uItemsListDesc[ptShowInfo->selectItem].textId);
            uint16_t y = CALC_MENU_ITEM_Y(currRow);
            text_show_menu_item(0, y, (char *)pItemName, MENU_FONT_SIZE, g_back_color, g_point_color);
        }
    }

    /* 记录本次选中项索引 */
    sg_lastSelectItem = ptShowInfo->selectItem;
    /* 清除刷新标志 */
    sg_needRefresh = false;
}

/* 按键回调函数 - 上键(KEY0) */
static void OnKey0FunCB(cotKey_t key)
{
    /* 处理按键短按和长按 */
    if (!cotKey_IsLongPress(key))
    {
        if (cotKey_IsTriggerWayMet(key, COT_KEY_TRIGGER_WAY_PRESS)) /* 按下首次触发 */
        {
            /* 预留：首次按下按键时的处理 */
        }
        if (cotKey_IsTriggerWayMet(key, COT_KEY_TRIGGER_WAY_LOSSEN)) /* 识别到短按 */
        {
            cotMenu_SelectPrevious(true);
            sg_needRefresh = true;
        }
    }
    else
    {
        if (cotKey_IsTriggerWayMet(key, COT_KEY_TRIGGER_WAY_PRESS)) /* 识别到长按 */
        {
            cotMenu_SelectPrevious(true);
            sg_needRefresh = true;
        }
        if (cotKey_IsTriggerWayMet(key, COT_KEY_TRIGGER_WAY_LOSSEN)) /* 长按松开 */
        {
            /* 预留：长按按键松手时的处理 */
        }
    }
}

/* 按键回调函数 - 下键(KEY1) */
static void OnKey1FunCB(cotKey_t key)
{
    /* 处理按键短按和长按 */
    if (!cotKey_IsLongPress(key))
    {
        if (cotKey_IsTriggerWayMet(key, COT_KEY_TRIGGER_WAY_PRESS)) /* 按下首次触发 */
        {
            /* 预留：首次按下按键时的处理 */
        }
        if (cotKey_IsTriggerWayMet(key, COT_KEY_TRIGGER_WAY_LOSSEN)) /* 识别到短按 */
        {
            cotMenu_SelectNext(true);
            sg_needRefresh = true;
        }
    }
    else
    {
        if (cotKey_IsTriggerWayMet(key, COT_KEY_TRIGGER_WAY_PRESS)) /* 识别到长按 */
        {
            cotMenu_SelectNext(true);
            sg_needRefresh = true;
        }
        if (cotKey_IsTriggerWayMet(key, COT_KEY_TRIGGER_WAY_LOSSEN)) /* 识别到长按 */
        {
            /* 预留：长按按键松手时的处理 */
        }
    }
}

/* 按键回调函数 - 确认键(KEY2) */
static void OnKey2FunCB(cotKey_t key)
{
    /* 处理按键短按和长按 */
    if (!cotKey_IsLongPress(key))
    {
        if (cotKey_IsTriggerWayMet(key, COT_KEY_TRIGGER_WAY_LOSSEN)) /* 识别到短按 */
        {
            cotMenu_Enter();
        }
    }
    else
    {
        if (cotKey_IsTriggerWayMet(key, COT_KEY_TRIGGER_WAY_PRESS)) /* 识别到长按 */
        {
            /* 逐级回到主菜单 */
            while (cotMenu_Exit(true) == 0)
            {
            }
        }
    }
}

/* 进入回调函数 */
void OnDisplaySettingMenuEnter(const cotMenuItemInfo_t *ptItemShowInfo)
{
    cotMenu_Bind(sg_DisplaySettingMenuTable, COT_GET_MENU_NUM(sg_DisplaySettingMenuTable), ShowDisplaySettingMenu);
}

/* 加载回调函数 */
void LoadDisplayMenuSetting(const cotMenuItemInfo_t *ptItemShowInfo)
{
    /* 注册按键回调 */
    cotKey_Listen(KEY0, OnKey0FunCB);
    cotKey_Listen(KEY1, OnKey1FunCB);
    cotKey_Listen(KEY2, OnKey2FunCB);

    /* 重置KEY2为默认配置，避免配置冲突 */
    cotKey_ResetDefaultConfig(KEY2);

    /* 配置按键模式 */ 
    cotKey_ConfigLongPressMode(KEY0, 1200, 400); /* 上键长按1.2秒触发,每0.4秒重复一次  */
    cotKey_ConfigLongPressMode(KEY1, 1200, 400); /* 下键长按1.2秒触发,每0.4秒重复一次 */
    cotKey_ConfigLongPressMode(KEY2, 2000, 0);   /* 设置SET键2秒长按，不重复触发 */
    
    /* 标记需要刷新且全屏刷新 */
    sg_needRefresh = true;
    needFullRefresh = true;
}

/* 菜单任务回调 */
void DisplaySettingMenuTask(const cotMenuItemInfo_t *ptItemShowInfo)
{
    /* 预留：可在此添加菜单定时刷新、动画等功能 */
}
